# Load and install only the libraries actually used in HW-03if (!require("pacman")) install.packages("pacman")pacman::p_load(# Core tidyverse + file/path handling tidyverse, # dplyr, ggplot2, readr, etc. here, # relative file paths janitor, # clean_names() glue, # string interpolation lubridate, # dates scales, # percent_format(), comma()# Plotting & annotations ggrepel, # better geom_text labels ggthemes, # extra themes cowplot, # for draw_plot() and draw_grob() ggpmisc, # for stat_poly_eq etc. (optional but fine to keep) colorspace, # accessibility palettes# Image & raster jpeg, # to read JPEG parchment grid, # for rasterGrob()# Likert & challenge-specific dsbox, # gglikert() ggstats # used in setup/testing)# Global plot styleggplot2::theme_set(ggplot2::theme_minimal(base_size =14))# Console display widthoptions(width =65)# Global chunk settingsknitr::opts_chunk$set(fig.width =7,fig.asp =0.618,fig.retina =3,fig.align ="center",dpi =300)
1 - Du Bois challenge.
Code
# source of parchment background: https://pixabay.com/photos/paper-old-texture-parchment-1074131/# Parchment backgroundpaper <-readJPEG(here("data", "paper.jpg"))# Convert image to a raster objectpaper_raster <-rasterGrob(paper, width =unit(1, "npc"), height =unit(1, "npc"))# Read income dataincome <-read_csv(here("data", "income.csv"))#Fix Label downstreamincome <- income |>mutate(Class =recode(Class,"$1000 AND OVER"="$1,000 AND OVER","$750-1000"="$750-1,000" ))# Cleanincome_long <- income |>mutate(# Fix specific values for bottom classTax =if_else(Class =="$100-200", 0.1, Tax),Other =if_else(Class =="$100-200", 9.9, Other) )|>pivot_longer(cols = Rent:Other,names_to ="category",values_to ="percent_income" ) |>filter(percent_income !=0) |>mutate(category =toupper(category), # Capitalize category for legendClass =fct_relevel(Class, "$1,000 AND OVER","$750-1,000","$500-750","$400-500","$300-400","$200-300","$100-200" ),category =fct_relevel(category, # Reorder spending categories"OTHER", "TAX", "CLOTHES", "FOOD", "RENT" ) )# Add label colorincome_long <- income_long |>mutate(text_color_group =if_else(category =="RENT", "white", "black") )
Code
# Inspired by: https://rpubs.com/leeolney/duboischallenge# Main plot objectincome_plot <-ggplot( income_long, # Use the cleaned long-format income dataaes(x = percent_income, y = Class, fill = category) # Stack percentage by category within each income Class) +# Bar Chart Objectgeom_bar(stat ="identity", # Use actual percent_income valuesposition ="fill", width =0.5, color ="black", # Outlinesize =0.2 ) +# Percentage centered inside bar labelsgeom_text(data = income_long |>filter(!(Class =="$100-200"& category =="TAX")), # Exclude 0.1% labelaes(label =paste0(percent_income, "%"), color = text_color_group), # White or black depending on fillposition =position_fill(vjust =0.5), # Center in blocksize =3, fontface ="bold", show.legend =FALSE# Do not show in legend ) +# Right side y-axis geom_text(data = income_long |>distinct(Class, Average_Income),aes(x =1.07, y = Class, label =paste0("$", comma(Average_Income, accuracy =1))),inherit.aes =FALSE,hjust =0,size =4,family ="mono",color ="black") +# Left side y-axis geom_text(data = income_long |>distinct(Class),aes(x =-0.1, y = Class, label = Class),inherit.aes =FALSE,hjust =1,size =4,family ="mono",color ="black") +# Left y-axis Class annotate("text",x =-0.1, # Slightly left of the start of barsy =7.7, # Just above topmost row (manual adjustment)label ="CLASS",hjust =1, # Left-alignedvjust =1, # Top-alignedsize =4,family ="mono",color ="black" ) +# Right y-axis Average income annotate("text",x =1.07, # Align with income labelsy =8, label ="AVERAGE\nINCOME", hjust =0,vjust =1,size =4,family ="mono",color ="black" ) +# Manual color to match example scale_fill_manual(name =NULL,values =c("OTHER"="#cbdfbd", # Greenish"TAX"="#8e9aaf", # Blue-gray"CLOTHES"="#d78879", # Coral"FOOD"="#a08294", # Purple-gray"RENT"="black"# Black for rent ),guide =guide_legend(reverse =TRUE) # Reverse order to match bar stacking ) +scale_color_manual(values =c("black"="black", "white"="white") # Need to define for color text_color_group) +# Scalingscale_x_continuous(expand =expansion(mult =c(0.25, 0.25)) # Add space left/right of bars ) +theme_void() +# Void and add back what is neededtheme(legend.position ="top", legend.box.margin =margin(t =20), # Push legend down from top of plotplot.title =element_text(hjust =0.5,face ="bold" )) +labs(title ="INCOME AND EXPENDITURE OF 150 NEGRO FAMILIES IN ATLANTA, GA., U.S.A.",x =NULL,y =NULL )# Plot with parchment objectfinal_plot <- cowplot::ggdraw(xlim =c(0, 1), ylim =c(0, 1), clip ="off") +draw_grob(paper_raster, 0, 0, 2, 2) +draw_plot(income_plot)final_plot